package com.chif.mapreduce.Combiner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class WordCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        //指定hadoop运行时在本地的临时工作目录
        conf.set("hadoop.tmp.dir", "D:/tmp/mapreduce_tmp_cache");

        Job job = Job.getInstance(conf);

        //关联本Driver程序的jar
        job.setJarByClass(WordCountDriver.class);

        //关联Mapper
        job.setMapperClass(WordCountMapper.class);
        //指定需要使用combiner，以及用哪个类作为combiner的逻辑
        job.setCombinerClass(WordCountCombiner.class);
        //关联Reduce
        job.setReducerClass(WordCountReducer.class);

        //设置Mapper的输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //设置最终的输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //设置输入输出路径
        FileInputFormat.setInputPaths(job,new Path("C:\\Users\\Chef Liu\\Desktop\\Big\\hadoop\\input"));
        FileOutputFormat.setOutputPath(job,new Path("C:\\Users\\Chef Liu\\Desktop\\Big\\hadoop\\output"));

        //提交job
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);

    }
}
